package com.charonmc;

import com.charonmc.commands.AutoLogin;
import com.charonmc.commands.ChangePassword;
import com.charonmc.commands.Login;
import com.charonmc.commands.Register;
import com.charonmc.listeners.PlayerListenerLogin;
import com.charonmc.listeners.WorldListenerLogin;
import com.charonmc.utils.mysql.SqlManager;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * @author ChuYang
 */
public class CharonLogin extends JavaPlugin {
    public static CharonLogin instance;
    public static ArrayList<Player> loginPlayers = new ArrayList<>();
    public static HashMap<Player, Integer> noLoginPlayers = new HashMap<>();
    public static HashMap<Player, Integer> autoLoginPlayer = new HashMap<>();
    public static HashMap<Player, Integer> loginErrorTime = new HashMap<>();
    @Override
    public void onEnable() {
        instance = this;
        this.getCommand("Register").setExecutor(new Register());
        this.getCommand("Login").setExecutor(new Login());
        this.getCommand("AutoLogin").setExecutor(new AutoLogin());
        this.getCommand("ChangePassword").setExecutor(new ChangePassword());
        Bukkit.getPluginManager().registerEvents(new PlayerListenerLogin(), this);
        Bukkit.getPluginManager().registerEvents(new WorldListenerLogin(), this);
        loginTime.runTaskTimerAsynchronously(this, 0, 40);
        task.runTaskTimerAsynchronously(this, 0, 200);
    }

    BukkitRunnable task = new BukkitRunnable() {
        @SneakyThrows
        @Override
        public void run() {
            for (Player player : CharonLogin.autoLoginPlayer.keySet()) {
                Integer n = CharonLogin.autoLoginPlayer.get(player);
                n -= 200;
                CharonLogin.autoLoginPlayer.put(player, n);
                if (n < 0){
                    SqlManager sqlManager = new SqlManager("charonmc");
                    Object playerId = sqlManager.createQuery()
                            .inTable("players")
                            .selectColumns("player_id")
                            .addCondition("player_name", player.getPlayer().getName())
                            .build()
                            .execute()
                            .first();
                    sqlManager.createUpdate()
                            .inTable("player_set")
                            .addColumnNames(new String[]{"auto_login"})
                            .addParams(new Object[]{0})
                            .addCondition("player_id", playerId)
                            .build()
                            .execute();
                }
            }
        }
    };

    BukkitRunnable loginTime = new BukkitRunnable() {
        @Override
        public void run() {
            for (Player player : noLoginPlayers.keySet()) {
                Integer n = noLoginPlayers.get(player);
                n -= 2;
                noLoginPlayers.put(player, n);
                if (n <= 0){
                    BukkitRunnable kickTimeOutTask = new BukkitRunnable() {
                        @Override
                        public void run() {
                            player.kickPlayer("§c§l登录超时");
                        }
                    };
                    noLoginPlayers.remove(player);
                    Bukkit.getScheduler().runTask(CharonLogin.instance, kickTimeOutTask);
                }
            }
        }
    };
}
